﻿#region Copyright

// All Material Copyright © 2010 Dynamic Concepts Development Corp., All Rights Reserved.
//      Portions Copyright © 1984-2009 Dynamic Concepts Development Corp., All Rights Reserved.
// For further information please contact: SoftwareSupport@dynconcepts.com
// Dynamic Concepts Development Corp.  311 W 57th St. Suite #422, New York, NY 10019

#endregion

#region Using References

using System;
using System.Windows;
using System.Windows.Controls;
using DynConDevCorp.OpenArch.SupportLibrary.ApplicationInfrastructure;

#endregion

namespace DynConDevCorp.OpenArch.DynAppFramework.Tiers.UserInterface.WPF.UIElements
{
    /// <summary>
    /// Interaction logic for LabeledTextBlock.xaml
    /// </summary>
    public partial class LabeledTextBlock : UserControl
    {
        #region Constructors

        public LabeledTextBlock()
        {
            InitializeComponent();
            TheContent.LostFocus += TheContent_LostFocus;
        }

        #endregion

        #region Public Properies and Indexers

        public String Caption
        {
            get { return (String) GetValue(CaptionProperty); }
            set { SetValue(CaptionProperty, value); }
        }

        public int LabelWidth
        {
            get { return m_LabelWidth; }
            set
            {
                m_LabelWidth = value;
                TheLabel.Width = m_LabelWidth;
            }
        }

        public bool ReadOnly
        {
            get { return TheContent.IsReadOnly; }
            set { TheContent.IsReadOnly = value; }
        }

        public String Value
        {
            get { return (String) GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        #endregion

        #region Private Methods

        private static void OnCaptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LabeledTextBlock instance = (LabeledTextBlock) d;
            instance.TheLabel.Content = e.NewValue;
        }

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LabeledTextBlock instance = (LabeledTextBlock) d;
            QueuedLogger.GlobalInstance.Log(QueuedLogger.MessageSeverity.Debug, "OnValueChanged ({0}={1})", instance.Caption, e.NewValue);
            instance.TheContent.Text = (String) e.NewValue;
        }

        private void TheContent_LostFocus(object sender, RoutedEventArgs e)
        {
            QueuedLogger.GlobalInstance.Log(QueuedLogger.MessageSeverity.Debug, "TheContent_LostFocus ({0}={1})", TheLabel.Content, TheContent.Text);
            Value = TheContent.Text;
        }

        #endregion

        #region Static Fields and Constants

        private const string CaptionPropertyName = "Caption";
        private const string ValuePropertyName = "Value";

        public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(CaptionPropertyName, typeof (String), typeof (LabeledTextBlock), new PropertyMetadata(null, OnCaptionChanged));
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(ValuePropertyName, typeof (String), typeof (LabeledTextBlock), new PropertyMetadata(null, OnValueChanged));

        #endregion

        #region Instance Fields (Mutable)

        private int m_LabelWidth = 100;

        #endregion
    }
}